package com.example.demoweb.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demoweb.dao.CustomerRepository;
import com.example.demoweb.entity.Customer;

@RestController
@RequestMapping("customer")
public class CustomerController  extends BaseController{
	@Autowired
	private CustomerRepository repository;
	
	@GetMapping("/test")
	public List<Customer> test() {
		repository.deleteAll();

        // save a couple of customers
        repository.save(new Customer("Alice", "Smith"));
        repository.save(new Customer("Bob", "Smith"));

        // fetch all customers
        System.out.println("Customers found with findAll():");
        System.out.println("-------------------------------");
        for (Customer customer : repository.findAll()) {
            System.out.println(customer);
        }
        System.out.println();

        // fetch an individual customer
        System.out.println("Customer found with findByFirstName('Alice'):");
        System.out.println("--------------------------------");
        System.out.println(repository.findByFirstName("Alice"));

        System.out.println("Customers found with findByLastName('Smith'):");
        System.out.println("--------------------------------");
        List<Customer> cList = repository.findByLastName("Smith");
        for (Customer customer : cList) {
            System.out.println(customer);
        }
        return cList;
	}
}
